/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductAddEditPnl.java
 *
 * Created on Jul 17, 2011, 10:31:55 AM
 */
package com.viweb.storemanager.ui.product;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.MyListCellRenderer;
import com.viweb.storemanager.core.ObjectComboBoxModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.core.TextFieldUtils;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.BaseEntity;
import com.viweb.storemanager.entity.Category;
import com.viweb.storemanager.entity.Product;
import com.viweb.storemanager.entity.WareHouse;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class ProductAddEditPnl extends javax.swing.JPanel {

    private ProductPnl parent;
    private ArrayList<WareHouse> typeWareHouses;
    private ArrayList<Category> tyCategorys;
    private Product product;

    /** Creates new form ProductAddEditPnl */
    public ProductAddEditPnl() {
        initComponents();
    }

    public ProductAddEditPnl(ProductPnl parent) {
        this.typeWareHouses = Biz.getInstance().selectAllWareHouse();
        this.tyCategorys = Biz.getInstance().selectAllCategory();
        initComponents();
        this.parent = parent;
    }

    public void assignProduct(Product product) {
        this.product = product;

        if (this.product.getId() != -1) {
            this.txtName.setText(product.getName());
            TextFieldUtils.setIntegerValue(this.product.getNumber(), this.txtNumber);
            TextFieldUtils.setFloatValue((float) this.product.getPrice(), this.txtPrice);
            this.cbbWarehouse.setSelectedIndex(Biz.getObjectIndex((BaseEntity) this.product.getWarehouse(), this.typeWareHouses));
            this.cbbCatogories.setSelectedIndex(Biz.getObjectIndex((BaseEntity) this.product.getCategory(), this.tyCategorys));
            this.txtNotes.setText(product.getNote());
            this.lblDateModifierEdit.setText(product.getDatemodifies().toString());
            if (product.getStatus() != 0) {
                this.radioYes.setSelected(true);
            } else {
                this.radioNo.setSelected(true);
            }
        } else {
            this.txtName.setText(null);
            TextFieldUtils.setIntegerValue(this.product.getNumber(), this.txtNumber);
            TextFieldUtils.setFloatValue((float) this.product.getPrice(), this.txtPrice);
            this.txtNotes.setText(product.getNote());
            this.lblDateModifierEdit.setText(product.getDatemodifies().toString());
            this.txtNotes.setText(null);
            this.lblDateModifierEdit.setText(null);
            this.radioYes.setSelected(true);
        }
    }

    public void setEditableMode(boolean editable) {
        if (editable) {
            this.txtName.requestFocus();
        }
        this.txtName.setEditable(editable);
        this.txtPrice.setEditable(editable);
        this.txtNumber.setEditable(editable);
        this.txtNotes.setEditable(editable);
        this.btnSave.setEnabled(editable);
        this.cbbCatogories.setEditable(editable);
        this.cbbUnit.setEditable(editable);
        this.cbbWarehouse.setEditable(editable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupStatus = new javax.swing.ButtonGroup();
        lblCatogories = new javax.swing.JLabel();
        lblNotes = new javax.swing.JLabel();
        lblDateModifier = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblEmpty = new javax.swing.JLabel();
        scpNotes = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextPane();
        btnSave = new javax.swing.JButton();
        lblDateModifierEdit = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        cbbCatogories = new javax.swing.JComboBox();
        lblWarehouse = new javax.swing.JLabel();
        cbbWarehouse = new javax.swing.JComboBox();
        lblNumber = new javax.swing.JLabel();
        txtNumber = new javax.swing.JFormattedTextField();
        lblPrice = new javax.swing.JLabel();
        lblUnit = new javax.swing.JLabel();
        txtPrice = new javax.swing.JFormattedTextField();
        lblPriceCurrency = new javax.swing.JLabel();
        cbbUnit = new javax.swing.JComboBox();
        lblStatus = new javax.swing.JLabel();
        radioYes = new javax.swing.JRadioButton();
        radioNo = new javax.swing.JRadioButton();

        setEnabled(false);
        setLayout(new java.awt.GridBagLayout());

        lblCatogories.setText("Loại mặt hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblCatogories, gridBagConstraints);

        lblNotes.setText("Ghi chú:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblNotes, gridBagConstraints);

        lblDateModifier.setText("Ngày cập nhật:");
        lblDateModifier.setRequestFocusEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(lblDateModifier, gridBagConstraints);

        txtName.setEditable(false);
        txtName.setMinimumSize(new java.awt.Dimension(200, 25));
        txtName.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtName, gridBagConstraints);

        lblEmpty.setMinimumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setPreferredSize(new java.awt.Dimension(5, 50));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblEmpty, gridBagConstraints);

        txtNotes.setEditable(false);
        scpNotes.setViewportView(txtNotes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(scpNotes, gridBagConstraints);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/save.png"))); // NOI18N
        btnSave.setText("Lưu");
        btnSave.setToolTipText("Lưu thể loại vừa thêm mới hay hiệu chỉnh");
        btnSave.setEnabled(false);
        btnSave.setMaximumSize(new java.awt.Dimension(80, 26));
        btnSave.setMinimumSize(new java.awt.Dimension(80, 26));
        btnSave.setPreferredSize(new java.awt.Dimension(80, 26));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(btnSave, gridBagConstraints);

        lblDateModifierEdit.setMaximumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setMinimumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setPreferredSize(new java.awt.Dimension(200, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lblDateModifierEdit, gridBagConstraints);

        lblName.setText("Tên mặt hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblName, gridBagConstraints);

        cbbCatogories.setMinimumSize(new java.awt.Dimension(200, 22));
        cbbCatogories.setPreferredSize(new java.awt.Dimension(200, 22));
        ObjectComboBoxModel modelCat = new ObjectComboBoxModel(this.tyCategorys);
        cbbCatogories.setModel(modelCat);
        cbbCatogories.setRenderer(new MyListCellRenderer(22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(cbbCatogories, gridBagConstraints);

        lblWarehouse.setText("Kho:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblWarehouse, gridBagConstraints);

        cbbWarehouse.setMinimumSize(new java.awt.Dimension(200, 22));
        cbbWarehouse.setPreferredSize(new java.awt.Dimension(200, 22));
        ObjectComboBoxModel model = new ObjectComboBoxModel(this.typeWareHouses);
        cbbWarehouse.setModel(model);
        cbbWarehouse.setRenderer(new MyListCellRenderer(22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(cbbWarehouse, gridBagConstraints);

        lblNumber.setText("Số lượng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        add(lblNumber, gridBagConstraints);

        txtNumber.setEditable(false);
        txtNumber.setMinimumSize(new java.awt.Dimension(200, 25));
        txtNumber.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtNumber, gridBagConstraints);

        lblPrice.setText("Đơn giá:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblPrice, gridBagConstraints);

        lblUnit.setText("Đơn vị tính:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblUnit, gridBagConstraints);

        txtPrice.setEditable(false);
        txtPrice.setMinimumSize(new java.awt.Dimension(100, 25));
        txtPrice.setPreferredSize(new java.awt.Dimension(100, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtPrice, gridBagConstraints);

        lblPriceCurrency.setText("/ VNĐ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        add(lblPriceCurrency, gridBagConstraints);

        cbbUnit.setMinimumSize(new java.awt.Dimension(100, 22));
        cbbUnit.setPreferredSize(new java.awt.Dimension(100, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(cbbUnit, gridBagConstraints);

        lblStatus.setText("Tình trạng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblStatus, gridBagConstraints);

        btnGroupStatus.add(radioYes);
        radioYes.setText("Có");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioYes, gridBagConstraints);

        btnGroupStatus.add(radioNo);
        radioNo.setText("Không");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioNo, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            this.product.setName(this.txtName.getText().trim());
            this.product.setWarehouse((WareHouse) this.cbbWarehouse.getSelectedItem());
            this.product.setCategory((Category) this.cbbCatogories.getSelectedItem());
            this.product.setNumber(Integer.parseInt(txtNumber.getText().trim()));
            this.product.setPrice(Double.parseDouble(txtPrice.getText().trim()));
            this.product.setStatus((byte) (radioYes.isSelected() ? 1 : 0));
            //this.product.setUnit((cbbUnit.getSelectedItem());
            //TODO: don vi tinh su dung nhap vo
            this.product.setNote(this.txtNotes.getText().trim());
            boolean status = false;
            if (this.product.getId() != -1) {
                status = Biz.getInstance().insertProduct(product);
            } else {
                status = Biz.getInstance().updateProduct(product);
            }
            // Nếu cập nhật (thêm mới hoặc sữa thông tin) không thành công
            if (status) {
                OptionPane.show(JOptionPane.getFrameForComponent(ProductAddEditPnl.this), "Cập nhật không thành công! " + Biz.getInstance().getLastError(), "Thông báo", OptionPane.MsgTYPE_WARNING);
                // Nếu cập nhật thành công
            } else {
                if (this.product.getId() != -1) {
                    // Set id cho quốc gia vừa mới cập nhật (chỉ có ý nghĩa trong trường hợp thêm mới)
                    //this.customer.setId(id);
                    OptionPane.show(JOptionPane.getFrameForComponent(ProductAddEditPnl.this), "Thêm mới sản phẩm thành công!", "Thông báo");
                } else {
                    OptionPane.show(JOptionPane.getFrameForComponent(ProductAddEditPnl.this), "Cập nhật sản phẩm thành công!", "Thông báo");
                }
                this.parent.updateComponentsUI(this.product);
            }
            CursorManager.setDefaultCursor(this);
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroupStatus;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbCatogories;
    private javax.swing.JComboBox cbbUnit;
    private javax.swing.JComboBox cbbWarehouse;
    private javax.swing.JLabel lblCatogories;
    private javax.swing.JLabel lblDateModifier;
    private javax.swing.JLabel lblDateModifierEdit;
    private javax.swing.JLabel lblEmpty;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNotes;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblPriceCurrency;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblUnit;
    private javax.swing.JLabel lblWarehouse;
    private javax.swing.JRadioButton radioNo;
    private javax.swing.JRadioButton radioYes;
    private javax.swing.JScrollPane scpNotes;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextPane txtNotes;
    private javax.swing.JFormattedTextField txtNumber;
    private javax.swing.JFormattedTextField txtPrice;
    // End of variables declaration//GEN-END:variables
}
